package com.person.controller;

import com.person.common.R;
import com.person.dto.DishDto;
import com.person.dto.SetmealDto;
import com.person.pojo.Dish;
import com.person.pojo.PageBean;
import com.person.pojo.Setmeal;
import com.person.pojo.SetmealDish;
import com.person.service.DishService;
import com.person.service.SetmealDishService;
import com.person.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐
     */

    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.add(setmealDto);

        setmealDishService.add(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 套餐分页
     */
    @GetMapping("/page")
    public R<PageBean> page(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String name) {
        log.info("分页查找：{},{},{}", page, pageSize, name);
        PageBean pageBean = setmealService.page(page, pageSize, name);
        return R.success(pageBean);
    }

    /**
     * 修改套餐
     */

    //回显
    @GetMapping("/{id}")
    public R<SetmealDto> inquire(@PathVariable Long id) {
        log.info("要修改套餐的id是：{}", id);
        SetmealDto SetmealDto = setmealService.selectSetmeal(id);
        return R.success(SetmealDto);
    }

    /**
     * 停售
     */
    @PostMapping("/status/0")
    public R<String> stopSell(@RequestParam List<Long> ids) {

        log.info("更改停售状态ids：{}",ids);
        setmealService.update(ids);
        return R.success("修改成功");
    }
    /**
     * 起售
     */
    @PostMapping("/status/1")
    public R<String> startSell(@RequestParam List<Long> ids) {

        log.info("更改停售状态ids：{}",ids);
        setmealService.updateStart(ids);
        return R.success("修改成功");
    }


    /**
     * 删除套餐
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐{}",ids);
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 保存套餐
     */
    @PutMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("保存套餐：{}",setmealDto);
        setmealService.save(setmealDto);
        return R.success("修改成功");
    }


    /**
     * 前台显示
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Long categoryId){
        List<SetmealDto> list = setmealService.list(categoryId);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> listDishes(@PathVariable Long id){
        List<SetmealDish> setmealDishes = setmealDishService.inquire(id);
        return R.success(setmealDishes);
    }

}
